package com.xmxsuperstar.app.ttac;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.xmxsuperstar.app.ttc.client.TwitterAllListClient;
import com.xmxsuperstar.app.ttc.client.model.TwitterList;

public class TwitterAllListActivity extends TTACActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		TwitterAllListClient tac = new TwitterAllListClient();

		List<TwitterList> allList = tac.getAllList();

		TwitterListsAdapter tla = new TwitterListsAdapter(allList);

		ListView listView = new ListView(this);

		listView.setAdapter(tla);

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Intent i = new Intent();
				i.putExtra("list_id", id);
				i.setClass(TwitterAllListActivity.this,
						TwitterListActivity.class);
				TwitterAllListActivity.this.startActivity(i);
			}
		});

		setContentView(listView);

	}

	class TwitterListsAdapter extends BaseAdapter {

		private List<TwitterList> allList;

		TwitterListsAdapter(List<TwitterList> allList) {
			this.allList = allList;
		}

		@Override
		public int getCount() {
			return allList.size();
		}

		@Override
		public Object getItem(int position) {
			return allList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return allList.get(position).getId();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			LayoutInflater inflater = (LayoutInflater) TwitterAllListActivity.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			if (convertView == null) {
				convertView = inflater.inflate(
						android.R.layout.simple_expandable_list_item_1, parent,
						false);
			}

			TextView tv = (TextView) convertView;
			tv.setText(allList.get(position).getSlug());
			return tv;
		}
	}

}
